import WordsReader from '../utils/WordsReader';
import ToolKit from '../utils/ToolKit';

/*eslint-disable*/
export default class ChinaDistrict {
    constructor() {
    }

    buildParentChildren = (parentList, totalList, childCodeLen) => {
        //5.处理相邻两级行政区
        return parentList.map((parent) => {
            const children = totalList.filter((child) => {
                return (
                    child.code.length === childCodeLen
                    &&
                    child.code.startsWith(parent.code)
                );
            }).map((child) => {
                //子节指定父节点
                return {
                    ...child,
                    parent,
                };
            });
            //父节点指定子节点列表对象
            return {
                ...parent,
                children,
            };
        });
    }

    getDistrictTree = () => {
        return new Promise((resolve, reject) => {
            WordsReader.readWords('chn-district.txt')
                .then((itemRows) => {
                    const retList = [];
                    const lenSet = {};
                    const lenArray = [];
                    //1.删除末尾多余的0
                    itemRows.forEach((itemRow) => {
                        let code = itemRow.split('-')[0].replace(/(00)+$/g, '');    //删除末尾连续的0
                        let name = itemRow.split('-')[1];
                        let len = code.length;
                        lenSet[`L${len}`] = (lenSet[`L${len}`] || 0) + 1;
                        retList.push({
                            code: code,
                            name: name,
                            fullName: '',
                            parent: '',
                            children: [],
                        });
                    });
                    //2.排序
                    retList.sort((item1, item2) => {
                        return item1.code > item2.code ? 1 : -1;
                    });
                    //3.取长度类型
                    Object.keys(lenSet).forEach((key) => {
                        lenArray.push(parseInt(key.substring(1), 10));
                    });
                    lenArray.sort();
                    //4.构建一二级行政区
                    let firstLevelList = retList.filter(item => item.code.length === 2);
                    firstLevelList = this.buildParentChildren(firstLevelList, retList, 4);
                    //5.构建二三级行政区
                    firstLevelList = firstLevelList.map((list1Item) => {
                        let {children} = list1Item;
                        let tmp = this.buildParentChildren(children, retList, 6);
                        return {
                            ...list1Item,
                            children: tmp,
                        };
                    });
                    // console.log(firstLevelList);
                    resolve(firstLevelList);
                }).catch((e) => {
                reject(e);
            });
        });
    }

    getAllLeafs = () => {
        function lookupLeaf(leafs, startList) {
            if (!startList || startList.length === 0) return;
            startList.forEach((listItem) => {
                if (!listItem.children || listItem.children.length === 0) {
                    leafs.push(listItem);
                } else {
                    lookupLeaf(leafs,listItem.children);
                }
            })
        }

        return new Promise((resolve, reject) => {
            const leafs = [];
            this.getDistrictTree()
                .then((startList) => {
                    lookupLeaf(leafs, startList);
                    resolve(leafs);
                }).catch((e) => {
                reject(e);
            });
        });
    }
}
